#pragma once
#include <random>
#include <chrono>

#include <json/json.h>
#include "../DBServer/db_grpc_client.h"

class json_message_parsing
{
protected:
	Json::Value root;
	Json::Reader reader;
	dbClient client;

public:
	json_message_parsing() :client(
		grpc::CreateChannel(
			std::string("127.0.0.1") +
			":" +
			"50052",
			grpc::InsecureChannelCredentials()
		)
	) {}
	~json_message_parsing() {}

	std::string DistributeServer(const std::string& jm)
	{
		if (reader.parse(jm, root))
		{
			if (root["jsonID"] == "11")
			{
				return Login(root["jsonData"]);
			}
			if (root["jsonID"] == "21")
			{
				return Message(root["jsonData"]);
			}
			else
			{
				std::cerr << "error jsonID" << std::endl;
				return "error";
			}
		}
		else
		{
			std::cerr << "error message!" << std::endl;
			return "error";
		}
	}


	/*"Login": {
		"jsonID": "11",
			"jsonData" : {
			"user_id": 2110551202,
				"user_password" : "031207Gjb_"
		}
	}
		"LoginReturn": {
		"jsonID": "12",
		"jsonData": {
			"user_id": 2110551202,
			"status": 1,
			"error": "找不到该用户"
		}
	},





	*/

	std::string Login(const Json::Value& jmd)
	{
		int id = jmd["user_id"].asInt();
		std::string password = jmd["user_password"].asString();
		if (client.userSearchByID(id) == 1)
		{
			std::string getPassword = client.userGetPasswordByID(id);
			if (password == getPassword)
			{
				Json::Value r;
				r["jsonID"] = "12";
				Json::Value data;
				data["user_id"] = id;
				data["status"] = 1;
				data["error"] = "";
				r["jsonData"] = data;
				return r.toStyledString();
			}
			else
			{
				Json::Value r;
				r["jsonID"] = "12";
				Json::Value data;
				data["user_id"] = id;
				data["status"] = 2;
				data["error"] = "error password!";
				r["jsonData"] = data;
				return r.toStyledString();

			}
		}
		else
		{
			Json::Value r;
			r["jsonID"] = "12";
			Json::Value data;
			data["user_id"] = id;
			data["status"] = 3;
			data["error"] = "error id!";
			r["jsonData"] = data;
			return r.toStyledString();
		}
	}

	/*




	"Message": {
		"jsonID": "21",
		"jsonData": {
			"send_id": 2110551202,
			"receive_id": 2110551201,
			"time": "20231201T123502",
			"data": "hello-world",
			"status": 1
		}
	},
	"MessageReturn": {
		"jsonID": "22",
		"jsonData": {
			"id": 5,
			"status": 3,
			"time": "20231201T123502",
			"error": "对方离线"
		}
	},
	*/


	std::string Message(const Json::Value& jmd)
	{
		dbClient::message_log ml;
		ml.send_id = jmd["send_id"].asInt();
		ml.receive_id = jmd["receive_id"].asInt();;
		ml.time = jmd["time"].asString();
		ml.status = jmd["status"].asInt();
		unsigned seed1 = std::chrono::system_clock::now().time_since_epoch().count();
		std::mt19937 msID(seed1);
		ml.id = msID();
		ml.data = jmd["data"].asString();
		if (client.message_logInsert(ml))
		{
			Json::Value r;
			r["jsonID"] = "22";
			Json::Value data;
			data["id"] = ml.id;
			data["status"] = 1;
			data["time"] = ml.time;
			data["error"] = "";
			r["jsonData"] = data;
			return r.toStyledString();
		}
		else
		{
			Json::Value r;
			r["jsonID"] = "22";
			Json::Value data;
			data["id"] = 0;
			data["status"] = 3;
			data["time"] = ml.time;
			data["error"] = "error message!";
			r["jsonData"] = data;
			return r.toStyledString();
		}
	}



};